package comunications;

import gameState.Player;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.rmi.ConnectException;

import java.net.InetAddress;
import java.net.SocketException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.util.Properties;

import Graphic.DraftLayout;
import Graphic.WaitingForm;

import java.util.logging.Level;
import java.util.logging.Logger;
import server.ServerComunicateInterface;

public class runClient {

    static Logger logger = Logger.getLogger("testClient");

    public static void main(String[] args) {

        Player p;
        String curDir = System.getProperty("user.dir");
        String propertiesFile = "ServerProperties.properties";
        String serverIP = "";
        int serverPort = 0;

        try {
            Properties properties = new Properties();
            properties.load(new FileInputStream(propertiesFile));

            String myIP = InetAddress.getLocalHost().getHostAddress();
            serverIP = properties.getProperty("serverIP");
            serverPort = Integer.parseInt(properties.getProperty("serverPort"));
            String hostName = InetAddress.getLocalHost().getHostName();

//            final DraftLayout graphic= new DraftLayout();
//
//            System.out.println("--------------------------- RUN GRAPHIC() -------------------------------------------");
//    		//this.graphic.goGraphic();
//    		java.awt.EventQueue.invokeLater(new Runnable() {
//                public void run() {
//                    graphic.setVisible(true);
//                }
//            });

            WaitingForm waiting = WaitingForm.getInstance();
            waiting.switchOn();
            
            Client client = new Client();
            client.registryStartup();
            p = new Player(hostName, myIP, client.getPort(), 0);

            Thread.sleep(500);

            Registry registry = LocateRegistry.getRegistry(serverIP, serverPort);
            ServerComunicateInterface sci = (ServerComunicateInterface)registry.lookup("ServerComunicateImpl");
            sci.registration(p);
            
           
        }

        catch(FileNotFoundException fnfe){logger.log(Level.WARNING, "[testClient] File di configurazione ["+propertiesFile+"] non trovato in ["+curDir+"]");}
        catch(ConnectException ce){logger.log(Level.WARNING, "[testClient] Server non trovato all'indirizzo ["+serverIP+":"+serverPort+"]");}
        catch(IOException ioe){ioe.printStackTrace();}
        catch(Exception e){e.printStackTrace();}
    }
}
